package info.mykhaylov.dao;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

public interface BaseEntityDao {

	<T>
	void persist(final @NotNull T object);

	@Nullable
	<T>
	T load(final @NotNull Class<T> clazz, long id);

	@NotNull
	<T>
	List<? extends T> loadAll(final @NotNull Class<T> clazz);

	<T>
	void update(final @NotNull T object);

	<T>
	void delete(final @NotNull T object);
}

@Repository
class HibernateBaseEntityDao implements BaseEntityDao {
	@Autowired
	private HibernateTemplate hbmTemplate;

	@Override
	public <T> void persist(final @NotNull T object) {
		hbmTemplate.persist(object);
	}

	@Override
	@NotNull
	public <T> List<? extends T> loadAll(final @NotNull Class<T> clazz) {
		return hbmTemplate.loadAll(clazz);
	}

	@Override
	@Nullable
	public <T> T load(final @NotNull Class<T> clazz, long id) {
		return hbmTemplate.load(clazz, id);
	}

	@Override
	public <T> void update(@NotNull T object) {
		hbmTemplate.update(object);
	}

	@Override
	public <T> void delete(final @NotNull T object) {
		hbmTemplate.delete(object);
	}
}
